<?php


namespace App\Controller\Client;


use App\Controller\AbstractController;
use App\Request\Client\Employer\BalanceRequest;
use App\Request\Client\Employer\RechargeRequest;
use App\Service\Client\EmployerService;
use Hyperf\Di\Annotation\Inject;
use Swlib\Http\ContentType;
use Swlib\Saber;
use Swlib\SaberGM;

class EmployerController extends AbstractController
{
    /**
     * @Inject()
     * @var EmployerService
     */
    protected $employerService;

    public function info()
    {
        $data = $this->employerService->info();
        return $this->response->success($data);
    }


    public function cooperativeList()
    {
        $data = $this->employerService->cooperativeList();
        return $this->response->success($data);
    }

    /**
     *
     */
    public function balance(BalanceRequest $request)
    {
        $validator = $request->validated();
        $data = $this->employerService->balance($validator);
        return $this->response->success($data);
    }

    /**
     * @param RechargeRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function rechargePrepay(RechargeRequest $request)
    {
        $validator = $request->validated();
        $data = $this->employerService->rechargePrepay($validator);
        return $this->response->success($data);
    }

    public function rechargePrepayH5()
    {
        $data = $this->employerService->rechargePrepayH5();

//        $refererUrl = 'http://paytest.shopping98.com';
////        return $this->response->redirect($data['mweb_url'])->withHeader('Referer','http://paytest.shopping98.com')->withStatus(302);
//        vdump($data);
//        $header = [
//            'Accept-Language' => 'en,zh-CN;q=0.9,zh;q=0.8',
//            'Content-Type' => ContentType::HTML,
////            "Referer" => "http://98gongyi.com/",
//            'User-Agent' => "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
//        ];
//
//        $curl = curl_init();
//        curl_setopt($curl, CURLOPT_URL, $data['mweb_url']);
//        curl_setopt($curl, CURLOPT_HEADER, 0);
//        curl_setopt($curl, CURLOPT_TIMEOUT, 1);
//        curl_setopt ($curl, CURLOPT_REFERER, $refererUrl);
//
//        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
//        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
//        $data = curl_exec($curl);
//
//        if (curl_error($curl)) {
//            print "Error: " . curl_error($curl);
//        } else {
//            // 打印返回的内容
//            curl_close($curl);
//        }
//        vdump($data);
//        return $this->response->response()->withContent($data)->withHeader('Content-Type','text/html');

//        di(SaberGM::class)::->get()
//        return di(Saber::class)::create([
//                                            'base_uri' => $data['mweb_url'],
//                                            'headers' => [
//                                                'Accept-Language' => 'en,zh-CN;q=0.9,zh;q=0.8',
//                                                'Content-Type' => ContentType::HTML,
//                                                "Referer" => "http://98gongyi.com/",
//                                                'User-Agent' => "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
//                                            ]
//                                        ])->get('/');
//        return $this->response->response()->withHeader('Referer','http://98gongyi.com/')->withAddedHeader('Location', (string) $data['mweb_url'])->withStatus(302);
        return $this->response->success($data['mweb_url']);
    }

    public function rechargeCallback()
    {
        $body = $this->request->getParsedBody();
        $data = $this->employerService->rechargeCallback($body);
        return $this->response->response()->withHeader('Content-type', 'application/xml')->withContent($data);
    }


}